<template>
  <el-dialog
    title="分配任务"
    :close-on-click-modal="false" width="670px"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" inline @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="污水池" prop="productContainerId">
        <el-select filterable clearable disabled v-model="dataForm.productContainerId" placeholder="请选择污水池" style="width: 480px;" >
          <el-option v-for="container in containerList" :key="container.id" :label="container.name" :value="container.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="收运车辆" prop="dispatchCarId">
        <el-select filterable clearable v-model="dataForm.dispatchCarId" placeholder="请选择收运车辆" style="width: 480px;">
          <template v-for="car in carList">
            <el-option v-if="dataForm.ctUnitId === car.ctUnitId" :key="car.id" :label="car | getName" :value="car.id"></el-option>
          </template>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    containerList: {
      type: Array,
      default: () => []
    },
    carList: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      visible: false,
      dispatch: {},
      dataForm: {
        ctUnitId: '',
        productContainerId: '',
        dispatchCarId: ''
      },
      dataList: [],
      carDataList: [],
      dataRule: {
        productContainerId: [
          { required: true, message: '污水池不能为空', trigger: 'change' }
        ],
        dispatchCarId: [
          { required: true, message: '收运车辆不能为空', trigger: 'change' }
        ]
      }
    }
  },
    filters: {
    getName(car) {
      var numberPlate = car.numberPlate; 
      var driver = car.driver;
      var content = ""
    
      if (driver != null) {
       var  userName = driver.username
       var  mobile = driver.mobile
       content = userName +'('+mobile+')'
      }
      return numberPlate+'-'+content
    }
  },
  methods: {
    init (dispatch) {
      this.visible = true
      this.dispatch = dispatch
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        // const container = this.containerList.find(e => e.id === id)
        this.dataForm.productContainerId = this.dispatch.productContainerId
        this.dataForm.ctUnitId = this.dispatch.ctUnitId
        this.dataForm.dispatchCarId = ''
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sign/signtask/manual`),
            method: 'post',
            data: this.$http.adornData({
              dispatchId: this.dispatch.id,
              dispatchCarId: this.dataForm.dispatchCarId
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
